import 'package:flutter/material.dart';
import 'package:app/generated/app_strings.dart';
import 'package:app/src/http/bean/wroks/contract_bean.dart';
import 'package:app/src/http/bean/ywx_limit.dart';
import 'package:app/src/ui/app_ui.dart';

import '../../../../../http/bean/worksdo/material_requisition_bean.dart';
import '../../../../../manager/ywx_manager.dart';

///公用列表内容
class ContentList2 {
  static Widget ywxItem(
      BuildContext context, YwxItem? item, MaterialInfo data) {
    if (item?.ReviewType2 ==
        ReviewType.MaterialRequisition_OriginalRequisition) {
      //原片请购
      return _WorkOvertimeApproval(context, item, data);
    } else if (item?.ReviewType2 ==
        ReviewType.MaterialRequisition_AccessoriesRequisition) {
      //辅料请购
      return _WorkOvertimeApproval2(context, item, data);
    } else if (item?.ReviewType2 ==
        ReviewType.MaterialRequisition_HardwareRequisition) {
      //五金请购
      return _WorkOvertimeApproval3(context, item, data);
    } else if (item?.ReviewType2 == ReviewType.MaterialRequisition_Generated) {
      //物料请购：已生成
      return _WorkOvertimeApproval4(context, item, data);
    }
    return _WorkOvertimeApproval(context, item, data);
  }

  //last true 不带‘/’ fals 带‘/’ 即：是否为最后一个字符
  static Widget _text(String name, String? value, {bool last = false}) {
    var str = name + ":$value";
    List<InlineSpan>? children = null;
    if (!last) {
      children = [
        TextSpan(text: '/', style: AppStyle.textStyleWithEntryGrey()),
      ];
    }
    return Text.rich(TextSpan(
        style: AppStyle.textStyleWithEntry(), text: str, children: children));
    return Text(
      str,
      style: AppStyle.textStyleWithEntry(),
    );
  }

  //自动换行
  static Widget _wrap(List<Widget> children) {
    return Padding(
        padding: EdgeInsets.symmetric(vertical: 8),
        child: Wrap(
          alignment: WrapAlignment.start,
          children: children,
        ));
  }

  //物料请购;原片请购
  static Widget _WorkOvertimeApproval(
      BuildContext context, YwxItem? item, MaterialInfo materialInfo) {
    if (materialInfo.PurDemandList == null ||
        materialInfo.PurDemandList!.length <= 0) {
      return AppUi.getEmptyView();
    }
    var data = materialInfo.PurDemandList![0];
    //原片名称
    Widget MaterialName =
        _text(AppStrings.of(context).MaterialName0, data.MaterialName);
    //物料编码
    Widget MaterialCode =
        _text(AppStrings.of(context).MaterialCode, data.MaterialCode);
    //品牌
    Widget Brand = _text(AppStrings.of(context).Brand, data.ManufacturerName);
    //规格
    Widget Specifications =
        _text(AppStrings.of(context).Specifications, data.Size);
    //库存数量
    Widget InventoryNum =
        _text(AppStrings.of(context).InventoryNum, data.StockNum);
    //库存面积;只有原片有，辅料，五金没有。
    Widget InventoryArea =
        _text(AppStrings.of(context).InventoryArea, data.StockArea, last: true);
    return _wrap([
      MaterialName,
      MaterialCode,
      Brand,
      Specifications,
      InventoryNum,
      InventoryArea
    ]);
  }

  //物料请购;辅料请购
  static Widget _WorkOvertimeApproval2(
      BuildContext context, YwxItem? item, MaterialInfo materialInfo) {
    if (materialInfo.PurDemandList == null ||
        materialInfo.PurDemandList!.length <= 0) {
      return AppUi.getEmptyView();
    }
    var data = materialInfo.PurDemandList![0];
    //辅料名称
    Widget MaterialName =
        _text(AppStrings.of(context).MaterialName1, data.MaterialName);
    //物料编码
    Widget MaterialCode =
        _text(AppStrings.of(context).MaterialCode, data.MaterialCode);
    //品牌
    Widget Brand = _text(AppStrings.of(context).Brand, data.ManufacturerName);
    //规格
    Widget Specifications =
        _text(AppStrings.of(context).Specifications, data.Size);
    //库存数量
    Widget InventoryNum =
        _text(AppStrings.of(context).InventoryNum, data.StockNum, last: true);
    return _wrap([
      MaterialName,
      MaterialCode,
      Brand,
      Specifications,
      InventoryNum,
    ]);
  }

  //物料请购;五金请购
  static Widget _WorkOvertimeApproval3(
      BuildContext context, YwxItem? item, MaterialInfo materialInfo) {
    if (materialInfo.PurDemandList == null ||
        materialInfo.PurDemandList!.length <= 0) {
      return AppUi.getEmptyView();
    }
    var data = materialInfo.PurDemandList![0];
    //五金名称
    Widget MaterialName =
        _text(AppStrings.of(context).MaterialName2, data.MaterialName);
    //物料编码
    Widget MaterialCode =
        _text(AppStrings.of(context).MaterialCode, data.MaterialCode);
    //品牌
    Widget Brand = _text(AppStrings.of(context).Brand, data.ManufacturerName);
    //规格
    Widget Specifications =
        _text(AppStrings.of(context).Specifications, data.Size);
    //库存数量
    Widget InventoryNum =
        _text(AppStrings.of(context).InventoryNum, data.StockNum, last: true);
    return _wrap([
      MaterialName,
      MaterialCode,
      Brand,
      Specifications,
      InventoryNum,
    ]);
  }

  //物料请购;已生成
  static Widget _WorkOvertimeApproval4(
      BuildContext context, YwxItem? item, MaterialInfo materialInfo) {
    if (materialInfo.PurDemandList == null ||
        materialInfo.PurDemandList!.length <= 0) {
      return AppUi.getEmptyView();
    }
    if (materialInfo.PurDemandList!.length > 1) {
      List<Widget> allChildren = [];
      // //请购单号
      Widget PurchaseOrder = _text(
          AppStrings.of(context).PurchaseOrderNumber, materialInfo.PurCode,
          last: true);
      //children.add(PurchaseOrder);
      allChildren.add(PurchaseOrder);
      var SerialNumber = 1;
      materialInfo.PurDemandList?.forEach((element) {
        var data = element;
        List<Widget> children = [];
        // //请购单号
        // Widget PurchaseOrder = _text(
        //     AppStrings.of(context).PurchaseOrderNumber, materialInfo.PurCode);
        // children.add(PurchaseOrder);
        //物料类型
        Widget MaterialType = _text(
            "${SerialNumber++}.${AppStrings.of(context).TypeOfMaterial}",
            data.getMaterialTypeName(context));
        children.add(MaterialType);
        //物料名称
        Widget MaterialName =
            _text(AppStrings.of(context).MaterialName3, data.MaterialName);
        children.add(MaterialName);
        //项目名称
        Widget ProjectName =
            _text(AppStrings.of(context).ProjectName, materialInfo.ProjectName);
        children.add(ProjectName);
        //品牌
        Widget Brand =
            _text(AppStrings.of(context).Brand, data.ManufacturerName);
        children.add(Brand);
        //创建人
        Widget Founder =
            _text(AppStrings.of(context).Founder, materialInfo.CreaterName);
        children.add(Founder);
        //请购日期
        Widget DateRequest = _text(
            AppStrings.of(context).DateRequest, materialInfo.PurStartDate);
        children.add(DateRequest);
        //请购要货日期
        Widget DatePurchase = _text(
            AppStrings.of(context).PurchaseDate2, materialInfo.PurEndDate);
        children.add(DatePurchase);
        //状态
        Widget State =
            _text(AppStrings.of(context).State, materialInfo.StatusName, last: true);
        children.add(State);
        allChildren.add(_wrap(children));
      });
      return _wrap(allChildren);
    } else {
      var data = materialInfo.PurDemandList![0];
      List<Widget> children = [];
      //请购单号
      Widget PurchaseOrder = _text(
          AppStrings.of(context).PurchaseOrderNumber, materialInfo.PurCode);
      children.add(PurchaseOrder);
      //物料类型
      Widget MaterialType =
          _text(AppStrings.of(context).TypeOfMaterial, data.getMaterialTypeName(context));
      children.add(MaterialType);
      //物料名称
      Widget MaterialName =
          _text(AppStrings.of(context).MaterialName3, data.MaterialName);
      children.add(MaterialName);
      //项目名称
      Widget ProjectName =
          _text(AppStrings.of(context).ProjectName, materialInfo.ProjectName);
      children.add(ProjectName);
      //品牌
      Widget Brand = _text(AppStrings.of(context).Brand, data.ManufacturerName);
      children.add(Brand);
      //创建人
      Widget Founder =
          _text(AppStrings.of(context).Founder, materialInfo.CreaterName);
      children.add(Founder);
      //请购日期
      Widget DateRequest =
          _text(AppStrings.of(context).DateRequest, materialInfo.PurStartDate);
      children.add(DateRequest);
      //请购要货日期
      Widget DatePurchase =
          _text(AppStrings.of(context).PurchaseDate2, materialInfo.PurEndDate);
      children.add(DatePurchase);
      //状态
      Widget State =
          _text(AppStrings.of(context).State, materialInfo.StatusName, last: true);
      children.add(State);
      return _wrap(children);
    }
  }
}
